/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       combat_spell_registration.cpp

	$Header: /game/combat_spell_registration.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "combat_spell_registration.h"

#include <assert.h>
#include <memory.h>
#include "spell.h"

static t_spell_factory_base** get_create_functions()
{
	static t_spell_factory_base*  functions[k_spell_count];
	static bool	                  initialized = false;

	if (!initialized)
	{
		memset( functions, 0, sizeof( functions ));
		initialized = true;
	}
	return functions;
}

// -------------------------------------------------------------------
// register a combat spell
// -------------------------------------------------------------------
void t_combat_spell_registration_base::register_spell( t_spell spell, 
													   t_spell_factory_base* func )
{
	assert( get_create_functions()[spell] == 0 );
	get_create_functions()[spell] = func;
};

void t_combat_spell_registration_base::register_spells( t_spell const* spells, 
													    int count,
							                            t_spell_factory_base* func )
{
	while (count--)
	{
		assert( get_create_functions()[*spells] == 0 );
		get_create_functions()[*spells++] = func;
	}
}

// -------------------------------------------------------------------
// create a combat spell
// -------------------------------------------------------------------
t_combat_spell* create_combat_spell( t_battlefield& battlefield, t_spell spell)
{
	t_spell_factory_base* func;

	func = get_create_functions()[spell];

	if (func == 0)
		return 0;
	return func->create( battlefield, spell);
}
